﻿using System;
using Fly.Data;
using FlyWebFrame.Model;

namespace FlyWebFrame.Action
{
    public class category : homeaction
    {
        public int catId;
        public string schedule_detail;
        public string schedule_title;
        public string courses_title;
        public dynamic list;
        public dynamic features;
        public string abc;

        public override void Treat()
        {
            Int32.TryParse(GetParameter(1), out catId);
            current_link = "category/" + other_lang + "/" + catId;
            if (catId != 0) {
                Categories cat = new Categories();
                cat.id = catId;
                if (cat.Instance().Status) {
                    renderLeftNav(catId);
                    if (cat.parentId == 0) // bigcat
                    {
                        if (cat.type == 1)
                        {
                            renderFirstNews(cat.id);
                        }
                        if ((cat.type == 2) || (cat.type == 3 && cat.template == "newsdetail.htm"))
                        {
                            if (subcats != null) {
                                renderFirstNews(subcats[0].id);
                                current_title = subcats[0].name;
                            }
                        }
                        //if (cat.id == 9) // neu vao phan Dao tao thi mac dinh vao cat Muc tieu dao tao
                        //{
                        //    if (subcats != null)
                        //    {
                        //        renderFirstNews(33);
                        //        if (lang == "en") current_title = "Education purpose";
                        //        else current_title = "Mục tiêu đào tạo";
                        //    }
                        //}
                        if (cat.type == 3 && cat.template == "partners.htm") {
                            if (subcats != null)
                            {
                                renderListNews(subcats[0].id, 0, 15);
                            }
                        }
                        if (cat.type == 3 && cat.template == "clients.htm")
                        {
                            if (subcats != null)
                            {
                                renderListNews(subcats[0].id, 0, 15);
                            }
                        }
                        if (cat.type == 3 && cat.template == "schedule.htm")
                        {
                            renderSchedule();
                        }
                        if (cat.type == 3 && cat.template == "news.htm")
                        {
                            if (subcats != null)
                            {
                                int[] arr = new int[subcats.Length];
                                for (int i = 0; i < subcats.Length; i++)
                                {
                                    arr[i] = subcats[i].id;
                                }
                                renderListNews(arr, 0, 15);
                            }
                        }
                        if (cat.type == 2 && cat.template == "courses.htm")
                        {
                            if (subcats != null)
                            {
                                renderListNews(subcats[0].id, 0, 15);
                            }
                        }
                        
                    }
                    else {
                        if (cat.template == "trainingcourses.htm")
                        {
                            Courses c = new Courses();
                            list = c.Get().Data;
                        }
                        else if (cat.template == "schedule.htm")
                        {
                            renderSchedule();
                        }
                        if (cat.type == 1) {
                            renderFirstNews(catId);
                        }
                        if (cat.type == 2) {
                            renderListNews(cat.id,0,15);
                            if (cat.template == "courses.htm") {
                                News news = new News();
                                news.id = 73;
                                if (news.Instance().Status)
                                {
                                    schedule_detail = news.detail;
                                    schedule_title = news.title;
                                    if (lang == "en")
                                    {
                                        courses_title = "Courses";
                                    }
                                    else
                                    {
                                        courses_title = "Các khóa học";
                                        schedule_title = news.viTitle;
                                        schedule_detail = news.viDetail;
                                    }
                                }
                            }
                        }
                    }
                    if (cat.parentId == 1 || cat.id == 1)
                    {
                        News news = new News();
                        features = news.GetFeatured().Data;
                        if (features != null)
                        {
                            for (int i = 0; i < features.Length; i++)
                            {
                                features[i].description = Fly.Util.Text.Shorten(features[i].description, 45);
                                if (lang != "en")
                                {
                                    features[i].title = features[i].viTitle;
                                    features[i].description = Fly.Util.Text.Shorten(features[i].viDescription, 45);
                                    features[i].detail = features[i].viDetail;
                                    features[i].Date = this.ToVietnamDate(features[i].eventTime.DayOfWeek.ToString()) + ", " + features[i].eventTime.Day.ToString() + "/" + features[i].eventTime.Month.ToString() + "/" + features[i].eventTime.Year.ToString() + ", " + features[i].eventTime.Hour.ToString() + "h:" + features[i].eventTime.Minute.ToString();
                                }
                            }
                        }
                        content_right = "/html/home/righthome.htm";
                    }
                    else
                    {
                        content_right = "/html/home/right.htm";
                    }
                    
                }
            }
            if (catId == 23)
            {
                content_right = "/html/home/right.htm";
            }
        }

        public void renderSchedule()
        {
            Courses c = new Courses();
            dynamic courses = c.Get().Data;
            Schedule s = new Schedule();
            list = s.Get().Data;
            for (int i = 0; i < list.Length; i++)
            {
                for (int j = 0; j < courses.Length; j++)
                    if (courses[j].id == list[i].courseId)
                        list[i].course = courses[j].title;
                if (list[i].dayStart != 0)
                    list[i].time = list[i].dayStart + "/" + list[i].monthStart + "-" + list[i].dayEnd + "/" + list[i].monthEnd;
                else
                    list[i].time = "TBA";
            }
        }
    }
}